package com.yuyou.tas.report.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.report.service.IExamModelReportService;
import com.yuyou.tas.report.service.IStudentReportService;
import com.yuyou.tas.service.api.lesson.entity.Student;
import com.yuyou.tas.service.api.lesson.service.StudentAPI;
import com.yuyou.tas.service.api.report.dto.ReportHardDTO;
import com.yuyou.tas.service.api.report.dto.ReportKnow;
import com.yuyou.tas.service.api.report.dto.ReportKnowI;
import com.yuyou.tas.service.api.report.dto.ReportQuesDTO;
import com.yuyou.tas.service.api.report.dto.ReportScoreDTO;
import com.yuyou.tas.service.api.report.dto.ReportStudentDTO;
import com.yuyou.tas.service.api.report.dto.ReportStudentExtDTO;
import com.yuyou.tas.service.api.report.entity.ExamModelReport;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.PageParams;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.PaginationExt;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.tool.DefindBeanUtils;

@Api(value="StudentReportController",description="PC端学生报表接口")
@Validated
@RestController
@RequestMapping(value = "/api/student/")
public class StudentReportController extends BaseController{
	
	@Autowired
	private IStudentReportService studentReportService;

	@Autowired
	private IExamModelReportService examModelReportService;

	@Autowired
	private StudentAPI studentAPI;
	
	@ApiOperation(value = "学生报告列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageIndex", value = "pageSize", paramType = "query", required = true),
		@ApiImplicitParam(name = "pageSize", value = "pageSize", paramType = "query", required = true),
		@ApiImplicitParam(name = "studentId", value = "学生ID", paramType = "query", required = true),
		@ApiImplicitParam(name = "baseTrimesterId", value = "学期ID", paramType = "query", required = false),
		@ApiImplicitParam(name = "dataTimeTypeId", value = "期类型ID", paramType = "query", required = false)})
	@RequestMapping(value = "/reportStudentList", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<PaginationExt<ReportStudentDTO,ReportStudentExtDTO>> reportStudentList(HttpServletRequest request,
			@Validated @ModelAttribute PageParams pageParams,
			@NotNull(message = "学生ID不能为空") Long studentId,
			String baseTrimesterId,String dataTimeTypeId) throws Exception {	
		
		PaginationExt<ReportStudentDTO,ReportStudentExtDTO> res = new PaginationExt<>();  
		
		LoginRedisObj obj = this.getUserObj(request);		
		Long authOrganizationId=obj.getOrganizationId();
		
		Student student = studentAPI.selectByPrimaryKey(studentId);

		if(student==null){
			return ReturnResult.fail("没找到该学生信息");
		}
			
        ReportStudentExtDTO reportStudentExtDTO=new ReportStudentExtDTO(student.getName(),student.getStudentNo());
        res.setExtObj(reportStudentExtDTO);
        
        Map<String, Object> conditions = new HashMap<>();
        Pagination<ReportStudentDTO> queryPgn = new Pagination<>();
        conditions.put("studentId", studentId);
        conditions.put("authOrganizationId", authOrganizationId);
        conditions.put("baseTrimesterId", baseTrimesterId);
        conditions.put("dataTimeTypeId", dataTimeTypeId);
        
        conditions.put("terminal", null);
        queryPgn.setConditions(conditions);
        queryPgn.setPageParams(pageParams);

        int total = studentReportService.findReportStudentCount(queryPgn);
        queryPgn.setTotal(total);
        queryPgn.toBePage();
        if(total > 0){
            List<ReportStudentDTO> list = studentReportService.findReportStudentPageList(queryPgn);
            queryPgn.setList(list);    
        }
        DefindBeanUtils.copyProperties(queryPgn, res);
        return ReturnResult.success(res);
	}

	
	@ApiOperation(value = "01总体概况")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "reportId", value = "报告ID", paramType = "query", required = true)})
	@RequestMapping(value = "/reportInfo", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<ReportStudentDTO> reportInfo(HttpServletRequest request,
			@NotNull(message = "报告ID") Long reportId
			) throws Exception {	
		ReportStudentDTO res=studentReportService.reportInfo(reportId,true);		
		return ReturnResult.success(res);
	}

	@ApiOperation(value = "02成绩整体情况")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "reportId", value = "报告ID", paramType = "query", required = true)})
	@RequestMapping(value = "/reportScore", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<ReportScoreDTO> reportScore(HttpServletRequest request,
			@NotNull(message = "报告ID") Long reportId
			) throws Exception {
		ReportScoreDTO res=studentReportService.reportScore(reportId);
		return ReturnResult.success(res);
	}

	
	@ApiOperation(value = "03难易度情况")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "reportId", value = "报告ID", paramType = "query", required = true)})
	@RequestMapping(value = "/reportHard", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<ReportHardDTO> reportHard(HttpServletRequest request,
												  @NotNull(message = "报告ID") Long reportId) throws Exception {
		List<ReportHardDTO> res=studentReportService.reportHard(reportId);
		return ReturnResult.success(res);
	}

	@ApiOperation(value = "04各试题得分情况")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "reportId", value = "报告ID", paramType = "query", required = true)})
	@RequestMapping(value = "/reportQues", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<ReportQuesDTO> reportQues(HttpServletRequest request,
												  @NotNull(message = "报告ID") Long reportId) throws Exception {
		ReportQuesDTO res=studentReportService.reportQues(reportId);
		return ReturnResult.success(res);
	}

	@ApiOperation(value = "05知识得分情况分析")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "reportId", value = "报告ID", paramType = "query", required = true)})
	@RequestMapping(value = "/reportKnow", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<ReportKnow> reportKnow(@NotNull(message = "报告ID不能为空") Long reportId) throws Exception {
		ExamModelReport examModelReport = examModelReportService.selectByPrimaryKey(reportId);
		ReportKnow reportKnow = studentReportService.reportKnow(examModelReport.getStudentId(), examModelReport.getExamModelId(), examModelReport.getLessonClassId() ,examModelReport);
		return ReturnResult.success(reportKnow);
	}


	@ApiOperation(value = "05知识得分情况分析(文综)")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "reportId", value = "报告ID", paramType = "query", required = true),
			@ApiImplicitParam(name = "sysCode", value = "机构编号", paramType = "query", required = true)})
	@RequestMapping(value = "/reportKnowI", method = { RequestMethod.GET,RequestMethod.POST })
	public ReturnResult<ReportKnowI> reportKnowI(@NotNull(message = "报告ID不能为空") Long reportId){
		ExamModelReport examModelReport = examModelReportService.selectByPrimaryKey(reportId);
		ReportKnowI reportKnowI = studentReportService.reportKnowI(examModelReport.getStudentId(), examModelReport.getExamModelId(), examModelReport.getLessonClassId() ,examModelReport);
		return ReturnResult.success(reportKnowI);
	}

}
